package com.xingda.packing.events.lister;

import com.alibaba.fastjson.JSONObject;
import com.google.common.eventbus.Subscribe;
import com.xingda.packing.enums.*;
import com.xingda.packing.events.EventCreater;
import com.xingda.packing.events.dto.EventPackageFloorInfo;
import com.xingda.packing.events.dto.EventPackinvWeigh;
import com.xingda.packing.events.dto.EventTaskFinishe;
import com.xingda.packing.events.dto.PackageFloorInfoParam;
import com.xingda.packing.http.PackageSaveHttp;
import com.xingda.packing.http.PackinvweighHttp;
import com.xingda.packing.minitor.entity.PkEvents;
import com.xingda.packing.minitor.entity.PkTask;
import com.xingda.packing.minitor.service.IPkEventsService;
import com.xingda.packing.minitor.service.IPkTaskService;
import com.xingda.packing.minitor.service.IPkTaskUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
public class EventLister {

    @Resource
    IPkEventsService pkEventsService;

    @Resource
    IPkTaskService pkTaskService;

    @Resource
    IPkTaskUploadService pkTaskUploadService;

    @Resource
    EventCreater eventCreater;

    @Subscribe
    public void save(PkEvents pkEvents) {
        pkEventsService.save(pkEvents);
    }

    /**
     * 上传 每层轮子信息
     */
    @Subscribe
    public void send(EventPackageFloorInfo eventPackageFloorInfo) {
        int layer = eventPackageFloorInfo.getLayer();
        int boxLocationNo = BoxLocationNo.getById(eventPackageFloorInfo.getBoxLocationNo()).getNo();
        log.info("装箱位{},原始记录{}", boxLocationNo, eventPackageFloorInfo.getBoxLocationNo());
        PkTask pkTask = eventPackageFloorInfo.getPkTask();
        if (pkTaskUploadService.isUploadSuccess(pkTask.getId(), layer)) {
            log.warn("装箱位：{}，装箱号:{}，层号:{}已上传成功，不再上传！", boxLocationNo, pkTask.getBoxNo(), layer);
            eventCreater.createPkEvent(EventType.ALARM, eventPackageFloorInfo.getArea(), "装箱位：", boxLocationNo, "，装箱号：", pkTask.getBoxNo(), "，层号：", layer, "已上传成功，不再上传！");
            return;
        }
        PackageFloorInfoParam param = eventPackageFloorInfo.getParam();
        param.setPkPsnbasdoc("1004B2100000000PAKBL");
        JSONObject resultJson = PackageSaveHttp.save(param);
        TaskUploadState state = TaskUploadState.FAIL;
        if (!CollectionUtils.isEmpty(resultJson) && resultJson.getIntValue("code") == 0) {
            state = TaskUploadState.SUCCESS;
        } else {
            pkTaskUploadService.triggerUploadAlarm(eventPackageFloorInfo.getArea(), boxLocationNo,true);
        }
        eventCreater.createPkEvent(EventType.UPLOAD, eventPackageFloorInfo.getArea(), "装箱位：", boxLocationNo, "装箱号", pkTask.getBoxNo(), "，层号", layer, state.getDesc(), "！");
        pkTaskUploadService.saveOrUpdate(pkTask.getId(), layer, state.getNumber(), JSONObject.toJSONString(param), TaskUploadType.AUTO.getNumber(), resultJson.getString("msg"));
    }

    /**
     * 装箱完成，更新任务状态
     */
    @Subscribe
    public void updateTaskState(EventTaskFinishe eventFinishe) {
        PkTask pkTask = pkTaskService.getByBoxLocationNo(eventFinishe.getBoxLocationNo());
        pkTask.setState(BoxTaskState.FINISHED.getStateId());
        pkTask.setIsCurrentFinished(1);
        pkTask.setEndTime(new Date());
        pkTaskService.updateById(pkTask);
    }

    /**
     * 发送到信息中心，上传包装物信息
     */
    @Subscribe
    public void send(EventPackinvWeigh eventPackinvWeigh) {
        int boxLocationNo = BoxLocationNo.getById(eventPackinvWeigh.getBoxLocationNo()).getNo();
        PkTask pkTask = eventPackinvWeigh.getPkTask();
        int layer = 0;
        if (pkTaskUploadService.isUploadSuccess(pkTask.getId(), layer)) {
            log.warn("装箱位：{}，装箱号:{}，包装物已上传成功，不再上传！", boxLocationNo, pkTask.getBoxNo());
            return;
        }
        JSONObject resultJson = PackinvweighHttp.save(pkTask.getPkPacking());
        TaskUploadState state = TaskUploadState.FAIL;
        if (!CollectionUtils.isEmpty(resultJson) && resultJson.getIntValue("code") == 0) {
            state = TaskUploadState.SUCCESS;
        }
        eventCreater.createPkEvent(EventType.UPLOAD, eventPackinvWeigh.getArea(), "装箱位：", boxLocationNo, "装箱号", pkTask.getBoxNo(), "，包装物", state.getDesc(), "！");
        pkTaskUploadService.saveOrUpdate(pkTask.getId(), layer, state.getNumber(), null, TaskUploadType.AUTO.getNumber(), resultJson.getString("msg"));

    }

}
